<template>
	<div style="background-color: #F2F2F2;" v-loading="loading">
		<div class="physical-video">
			<video-player class="vjs-custom-skin" ref="videoPlayer" @ended="onPlayerEnded($event)" :options="playerOptions" :playsinline="true"></video-player>
		</div>

		<div class="examination">
			<div class="choose" :class="type == 0 ? 'chooseColor' : ''" @click="tabs(0)">
				<div>详情</div>
				<div :class="type == 0 ? 'chooseActive' : 'chooseActive2'"></div>
			</div>
			<div class="choose" :class="type == 1 ? 'chooseColor' : ''" @click="tabs(1)">
				<div>目录</div>
				<div :class="type == 1 ? 'chooseActive' : 'chooseActive2'"></div>
			</div>
		</div>

		<div v-if="type == 0">
			<div class="navs">
				<div>
					<div>{{ courseName }}</div>
					<div class="title2" style="text-align: right;">{{ createTime }}</div>
				</div>
			</div>
			<div class="navs">
				<div class="title">课程介绍</div>
				<div>{{ introduce }}</div>
			</div>
		</div>

		<div v-if="type == 1 && list[0].chapterFilePath">
			<div class="navs vue-btn" v-for="(item, index) in list" :key="index" @click="cut(index, item.chapterId)">
				<div>
					<div class="title">{{ item.chapterName }}</div>
					<div class="title2">{{ item.learnNum }}人已学习</div>
					<div class="title2">{{ item.chapterTime }}</div>
				</div>

				<div class="vue-flex white" v-if="currentIndex == index">
					<img class="icon" src="../../../../assets/img/a9.png" alt="" />
					<div class="title3">正在播放</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { courseInfo,finishChapter } from '../../../../network/api';
export default {
	name: 'studyDetail',
	data() {
		return {
			loading:true,
			
			playerOptions: {
				height: '460',
				autoplay: true,
				muted: false,
				language: 'en',
				playbackRates: [0.7, 1.0, 1.5, 2.0],
				sources: [
					{
						type: 'video/mp4',
						src: '' //课程视频
					}
				],
				// poster: 'https://surmon-china.github.io/vue-quill-editor/static/imgs/surmon-1.jpg'
			},

			id: '', //id
			id2: '', //章节id
			type: 0, //分类
			courseName: '', //课程标题
			introduce: '', //课程简介
			createTime: '', //课程创建时间

			currentIndex: 100000000, //章节视频索引
			list: [] //章节列表
		};
	},
	mounted() {
		// console.log(this.$route.query.id)
		this.id = this.$route.query.id;
		// this.playerOptions.sources[0].src = 'https://community-smart.oss-cn-beijing.aliyuncs.com/partyfiles/59bc47d9-f93f-4c66-8d3e-3d9ded974682.mp4';
		this.getMsg();
	},
	methods: {
		// 课程信息
		getMsg() {
			let param = {
				courseId: this.$route.query.id
			};
			courseInfo(param).then(res => {
				// console.log(res);
				this.courseName = res.retData.courseName;
				this.introduce = res.retData.introduce;
				this.createTime = res.retData.createTime;
				this.playerOptions.sources[0].src = res.retData.filePath;
				// this.playerOptions.sources[0].src = 'rtsp://61.151.156.6:8554/cam/realmonitor?vcuid=xz46DizuA1CQB812QVI60J&subtype=0&urlType=agentPull&manufacturer=Dahua&protocoltype=Dahua2&streamType=0&token=1635104794_c6c1201554e592a78e6b76c81f3883e56703a3bf&mapNet=ExtNet';
				this.list = res.retData.chapterList;
				
				this.loading = false
			});
		},

		// 切换分类
		tabs(e) {
			this.type = e;
		},

		// 切换小节
		cut(index,id2) {
			this.currentIndex = index
			this.id2 = id2;
			this.playerOptions.sources[0].src = this.list[index].chapterFilePath;
		},

		//监听媒体是否已到达结尾，播放完
		onPlayerEnded(player) {
			console.log(player);
			var that = this
			
			if(that.id2){
				let param = {
					courseId: that.id,
					chapterId:that.id2,
					userId:localStorage.getItem('userid')
				}
				finishChapter(param).then(res => {
					// 请求成功,重置
					courseInfo({courseId: that.id}).then(res => {
						// console.log(res);
						that.list = res.retData.chapterList;
					});
				});
			}
			
		}
	}
};
</script>
<style lang="scss" scoped>
.physical-video {
	width: 100%;
	height: 100%;
}

.examination {
	display: flex;
	justify-content: space-around;
	align-items: center;
	background-color: #ffffff;
}

.choose {
	text-align: center;
	padding-top: 12px;
}
.chooseColor {
	color: #cb1c1d;
}
.chooseActive {
	color: #cb1c1d;
	background-color: #cb1c1d;
	width: 36px;
	height: 4px;
	border-radius: 10px;
	margin-top: 8px;
}
.chooseActive2 {
	color: #000000;
	background-color: #ffffff;
	width: 36px;
	height: 4px;
	border-radius: 10px;
	margin-top: 8px;
}

/* type=0 */
.navs {
	background-color: #ffffff;
	margin-top: 10px;
	padding: 10px 15px;
	line-height: 160%;
	text-align: left;
}
.title {
	font-size: 16px;
	margin-bottom: 7px;
}
.time {
	margin-bottom: 5px;
}
.title2 {
	color: #999999;
}
.icon {
	width: 16px;
	height: 16px;
	margin-right: 5px;
}
.title3 {
	color: #cc1f20;
}
.white {
	white-space: nowrap;
}
</style>
